<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Page;

/**
 * 分销控制器
 * @author huajie <banhuajie@163.com>
 */
class BrokerageRecordController extends AdminController {
	public function _filter(&$map)
	{
		$order_sn=I('order_sn');
		if($order_sn!='')
		{
			$map['order_sn']=array('like','%'.$order_sn.'%');
		}
		$login_user=I('login_user');
		$userids=array();
		
		if(!empty($login_user))
		{
			$list=M('User')->where('login_user like "%'.$login_user.'%"')->select();
			if($list)
			{
				foreach($list as $item)
				{
					$userids[]=$item['id'];
				}
			}
			else
			{
				$userids[]=0;
			}
		}
		
		if(count($userids)>0)
		{
			$map['user_id']=array('in',implode(',',$userids));
		}
		$start_date=I('start_date');
		if(!empty($start_date))
		{
			$map['_string']=" addtime>='". strtotime($start_date)."'";
		}
		$end_date=I('end_date');
		if(!empty($end_date))
		{
			if(!empty($map['_string']))
			{
				$map['_string'].=" and addtime<='". strtotime($end_date)."'";
			}
			else
			{
				$map['_string']=" addtime<='". strtotime($end_date)."'";
			}
		}
	}
	
	public function index(){
		$userids = M('user')->getField('id',true);
		$excel_url = U('exportlist');
		$time_start = I('time_start');
		$time_end  = I('time_end');
		
		$map = array();
		if($time_start!=""){
			$start = strtotime($time_start);
			$map['addtime'] = array('egt',$start);
			$excel_url = "&start=".$start;
		}
		if($time_end!=""){
			$end = strtotime($time_end.' 23:59:59');
			$map['addtime'] = array('elt',$end);
			$excel_url = "&end=".$end;
		}
		if($time_start!=""&&$time_end!=""){
			$start = strtotime($time_start);
			$end = strtotime($time_end.' 23:59:59');
			$map['addtime'] = array('between',array($start,$end));
		}
		$map['user_id'] = array('in',$userids);
		$map['parent_id'] = array('in',$userids);
		$m = M('brokerage_record');
		$count = $m->where($map)->count();
		$p = new \Think\Page($count, 20);
		$list = $m->limit($p->firstRow.','.$p->listRows)->where($map)->order('id DESC')->select();
		foreach ($list as $k=>$v){
			$user = M('user')->where('id='.$v['user_id'])->find();
			$parent = M('user')->where('id='.$v['parent_id'])->find();
			$list[$k]['user'] = $user['mobile'];
			if($v['order_type']==1){
				$list[$k]['type'] = "商城消费";
			}else{
				$list[$k]['type'] = "拼团消费";
			}
			$list[$k]['parent'] = $parent['mobile'];
		}
		
		$page = $p->show ();
		$this->assign ( 'list', $list );
		$this->assign ( "page", $page );
		$this->assign('excel_url',$excel_url);
		
		$this->display();
	}
	/**
	 * 导出订单
	 */
	public function exportlist(){
		$m=M('brokerage_record');
		$userids = M('user')->getField('id',true);
		$map = array();
		$map['user_id'] = array('in',$userids);
		$map['parent_id'] = array('in',$userids);
		$start = I('start');
		$end = I('end');
		if($start!=""){
			$map['addtime'] = array('egt',$start);
		}
		if($end!=""){
			$map['addtime'] = array('elt',$end);
		}
		if($start!=""&&$end!=""){
			$map['addtime'] = array('between',array($start,$end));
		}
		
		$ids    =  rtrim(I('ids'),',');
		if (!empty($ids)){
			$map['id'] = array('in',$ids);
		}
		
		$list = $m->where($map)->order("id DESC")->select();
		// 		print_r($m->getLastsql());die;
		$str = '
		<table border="1">
		<tr style="background-color:#ccffcc;min-height:30px;">
		<th align="center">买家手机号</th>
		<th align="center">消费来源</th>
		<th align="center">订单编号</th>
		<th align="center">订单金额</th>
		<th align="center">得佣金者</th>
		<th align="center">所得佣金</th>
		<th align="center">返佣时间</th>
		</tr>
		';
		foreach ($list as $k => $v) {
			$user = M('user')->where('id='.$v['user_id'])->find();
			$parent = M('user')->where('id='.$v['parent_id'])->find();
			
			$str .='<tr style="min-height:30px;">';
			$str .= '
			<td align="center">'.$user['mobile'].'&nbsp;</td>';
			if($v['order_type']==1){
				$str .='<td align="center">商城消费</td>';
			}else{
				$str .='<td align="center">拼团消费</td>';
			}
			$str .='<td align="center">'.$v['order_sn'].'</td>
				<td align="center">'.$v['order_money'].'</td>
				<td align="center">'.$parent['mobile'].'</td>
				<td align="center">'.$v['money'].'</td>
			';
			$str .= '<td align="center">'.date('Y-m-d',$v['addtime']).'</td>';
			
			$str .='</tr>';
		}
		$str .="</table>";
		print_r($str);die;
		//输出
		header('Content-Length: '.strlen($str));
		header("Content-type:application/vnd.ms-excel;charset=UTF-8");
		header("Content-Disposition:filename=spell_orders.xls");
		echo $str;
	}
	
}
